package com.kevin.service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kevin.common.util.tree.TreeUtil;
import com.kevin.service.entity.SysMenu;
import com.kevin.service.mapper.SysMenuMapper;
import com.kevin.service.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单管理(SysMenu)表服务实现类
 *
 * @author makejava
 * @since 2023-03-28 19:32:30
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> getUserMenuList(Long userId, boolean superAdmin, Integer type) {
        return TreeUtil.build(superAdmin ? baseMapper.getMenuList(type) : baseMapper.getUserMenuList(userId, type), 0L);
    }

    @Override
    public List<String> getUserAuthority(Long userId, boolean superAdmin) {
        // 系统管理员，拥有最高权限
        List<String> authorityList = superAdmin ? baseMapper.getAuthorityList() : baseMapper.getUserAuthorityList(userId);
        // 用户权限列表
        List<String> permsSet = new ArrayList<>();
        for (String authority : authorityList) {
            if (StrUtil.isBlank(authority)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(authority.trim().split(",")));
        }
        return permsSet.stream().distinct().collect(Collectors.toList());
    }

}

